package com.tx.review.generic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 测试泛型上下边界
 *
 * @author ：tx
 * @version:
 */
public class Test04 {
    public static void main(String[] args) {
        Company company = new Company();
        ArrayList<Number> integers = new ArrayList<>();
        integers.add(1);
        company.show01(integers);

        //就算Integer是Number的子类，也不能够调用
//        ArrayList<Integer> strings = new ArrayList<>();
//        strings.add(11);
//        company.show01(strings);

        //如何使用？
        Company c2 = new Company();
        ArrayList<Number> i2 = new ArrayList<>();
        i2.add(1);
        company.show02(i2);
        ArrayList<String> s2 = new ArrayList<>();
        s2.add("abc");
        company.show02(s2);

        //测试泛型上界
        Company c3= new Company();
        ArrayList<Number> i3 = new ArrayList<>();
        i3.add(1);
        company.show03(i3);
        ArrayList<Integer> s3 = new ArrayList<>();
        s3.add(11);
        company.show03(s3);

        Company c4= new Company();
        ArrayList<Number> i4 = new ArrayList<>();
        i4.add(1);
        company.show04(i4);
        ArrayList<Integer> s4 = new ArrayList<>();
        s4.add(11);
        company.show04(s4);
        ArrayList<Long> l4 = new ArrayList<>();
        l4.add(11L);
//        company.show04(l4); //报错



    }
}

class Company{

    void show01(ArrayList<Number> list){
        System.out.println("普通输出：" + list.get(0));
    }

    // 泛型通配符（？）测试
    void show02(ArrayList<?> list){
        System.out.println("泛型通配符输出：" + list.get(0));
    }

    //测试泛型上界
    //只接受ArrayList<>泛型为Number及其子类的数据
    void show03(ArrayList<? extends Number> list){
        System.out.println("泛型通配符上界输出：" + list.get(0));
    }

    //测试泛型下界
    //只接受ArrayList<>泛型只能是Integer或者Integer的父类
    void show04(ArrayList<? super Integer> list){
        System.out.println("泛型通配符下界输出：" + list.get(0));
    }

}
